<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */


/**
 * Smarty {pp_inputradio} function plugin
 *
 * File:       function.pp_inputradio.php<br>
 * Type:       function<br>
 * Name:       pp_inputradio<br>
 * Date:       24.Feb.2003<br>
 * Purpose:    Prints out a list of text input types<br>
 * Input:<br>
 *           - name       (optional) - string default "text"
 *           - values     (required) - array
 *           - options    (optional) - associative array
 *           - checked    (optional) - array default not set
 *           - separator  (optional) - ie <br> or &nbsp;
 *           - output     (optional) - the output next to each text button
 *           - assign     (optional) - assign the output as an array to this variable
 * Examples:
 * <pre>
 * {pp_inputradio values=$ids output=$names}
 * {pp_inputradio values=$ids name='box' separator='<br>' output=$names}
 * {pp_inputradio values=$ids checked=$checked separator='<br>' output=$names}
 * </pre>
 * @link http://smarty.php.net/manual/en/language.function.html.texts.php {pp_inputradio}
 *      (Smarty online manual)
 * @author     Christopher Kvarme <christopher.kvarme@flashjab.com>
 * @author credits to Monte Ohrt <monte at ohrt dot com>
 * @version    1.0
 * @param array
 * @param Smarty
 * @return string
 * @uses smarty_function_escape_special_chars()
 */
function smarty_function_pp_inputradio($params, &$smarty)
{
	require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');

	$name = NULL;
	$value = NULL;
	$text = NULL;
	$extra = NULL;
    $separator = '';
    $label = true;
    $label_ids = false;
    $output = NULL;
	$require = NULL;
	$requirement = NULL;
	
	foreach($params as $_key => $_val) {
		switch($_key) {
			case 'name':
				$$_key = (string)$_val;
				break;
			
			case 'value':
				$$_key = (string)$_val;
				break;
			
			case 'text':
			case 'output':
				$$_key = (string)$_val;
				break;
			
            case 'checked':
            case 'selected':
                if(is_array($_val)) {
                    $smarty->trigger_error('pp_inputradios: the "' . $_key . '" attribute cannot be an array', E_USER_WARNING);
                } else {
                    $selected = (string)$_val;
                }
                break;
			
			case 'require':
				$$_key = (string)$_val;
				break;
				
			case 'requirement':
				$$_key = (string)$_val;
				break;
			
            case 'label':
            case 'label_id':
                $$_key = (bool)$_val;
                break;
			
			case 'assign':
				break;
			
			default:
				if( !is_array($_val) ) {
					$extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
				} else {
					$smarty->trigger_error("pp_inputradio: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
				}
				break;
		}
	}

	//
	$_html_result = smarty_function_pp_inputradio_output($name, $value, $selected, $output, $require, $requirement, $extra, $separator, $label, $label_id);

	return $_html_result;
}

function smarty_function_pp_inputradio_output($name, $value, $selected, $output, $require, $requirement, $extra, $separator, $label, $label_id) {
	$_output = '';
	
	//if( isset($_REQUEST[$name]) ) {
	//	$value = (string)$_REQUEST[$name];
	//}
	if( strstr($name, '[]') === false ) {
		if( isset($_REQUEST[$name]) ) {
			$value = (string)$_REQUEST[$name];
		}
	} else {
		$nameb = str_replace('[]', '', $name);
		if( !isset($_REQUEST[$nameb . '_index']) ) {
			$_REQUEST[$nameb . '_index'] = 0;
		}
		if( isset($_REQUEST[$nameb][$_REQUEST[$nameb . '_index']]) ) {
			$value = (string)$_REQUEST[$nameb][$_REQUEST[$nameb . '_index']];
		}
		$_REQUEST[$nameb . '_index']++;
	}
	
    if ($label) {
      if($label_id) {
          $_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!', '_', $name . '_' . $value));
          $_output .= '<label for="' . $_id . '" class="fb">';
      } else {
          $_output .= '<label class="fb">';
      }
   }
	$_output .= '<input type="radio" name="' . smarty_function_escape_special_chars($name) . '" value="' . smarty_function_escape_special_chars($value) . '"';
	if( $selected == 1 || (string)$value==$selected ) {
		$_output .= ' checked="checked" ';	
	}
	$_output .= $extra . '>' . $output;
	if( $require == true ) {
		if( $_REQUEST['_SUBMITTED_'] == 1 ) {
			if( isset($_REQUEST[$name]) && $_REQUEST[$name] == "" ) {
				if( $requirement ) {
					$_output .= '<span class="inputRequirement">' . $requirement . '</span>';
				} else {
					$_output .= '<span class="inputRequirement">*</span>';
				}
			} elseif( isset($_REQUEST[$name]) && $_REQUEST[$name] != "" ) {
				//
			}
		} else {
			if( $requirement ) {
				$_output .= '<span class="inputRequirement">' . $requirement . '</span>';
			} else {
				$_output .= '<span class="inputRequirement">*</span>';
			}
		}
	}
    if ($label) $_output .= '</label>';
    $_output .=  $separator;
	
	return $_output;
}
//
?>